from django.contrib.auth.models import User
from django.db import models

# Create your models here.
from utils.modelsMixin import ModelSetMixin

class UserDetail(ModelSetMixin):
    SEX_CHOICES = (
        (0,'女'),
        (1,'男'),
    )
    avatar = models.TextField(null=True,blank=True,verbose_name='头像')
    phone = models.CharField(null=True,blank=True,verbose_name='手机号',max_length=11,unique=True)
    age = models.IntegerField(null=True,blank=True,verbose_name='年龄')
    sex = models.IntegerField(null=True,blank=True,verbose_name='性别',choices=SEX_CHOICES)
    birthday = models.DateField(null=True,blank=True,verbose_name='生日')

    user = models.OneToOneField(User,on_delete=models.DO_NOTHING)

    class Meta:
        db_table = 'user_detail'
        verbose_name = '用户详情'
        verbose_name_plural = verbose_name


class Area(models.Model):#模型不需要有更新时间修改时间以及逻辑删除，可以直接继承models.Model
    name = models.CharField(max_length=20,verbose_name='名称')
    parent = models.ForeignKey('self',on_delete=models.SET_NULL,null=True,blank=True,verbose_name='上级行政区划') #外键关联自身表的id，传第一个参数为'self',第二个参数为删除后设置为NULL，第三个参数为允许为空值，第四个参数为允许前端不传参数，第五个参数为verbose_name

    class Meta:
        db_table = 'area'
        verbose_name = '行政区划'
        verbose_name_plural = verbose_name\

class Address(ModelSetMixin):#继承ModelSetMixin 需要修改时间 创建时间
    name = models.CharField(max_length=40,verbose_name='地址名')
    receiver = models.CharField(max_length=40,verbose_name='收货人')
    province = models.ForeignKey(Area,on_delete=models.PROTECT,verbose_name='省',related_name='province_addresses') #ondelete设置为删除字段保护
    city = models.ForeignKey(Area,on_delete=models.PROTECT,verbose_name='市',related_name='city_addresses')
    district = models.ForeignKey(Area,on_delete=models.PROTECT,verbose_name='区',related_name='district_addresses')
    place = models.CharField(max_length=40,verbose_name='详情地址')
    mobile = models.CharField(max_length=11,verbose_name='手机号码')

    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户') #ondelete设置为删除用户后删除收货地址

    class Meta:
        ordering = ['-update_time'] #默认更新地址显示最上层
        db_table = 'address'
        verbose_name = '收货地址'
        verbose_name_plural = verbose_name



